package com.chuji.crmcj.act;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.MagSendNoticeInfo;
import com.chuji.crmcj.bean.MessageAllUserInfo;
import com.chuji.crmcj.utils.CommonUtil;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.util.ArrayList;
import java.util.List;

public class NewNoticeActivity extends BaseActivity implements View.OnClickListener{
    //返回
    private ImageView mIv_Back;
    //数据存储
    List<MagSendNoticeInfo.ApiParamObjEntity> magSendNoticeData;
    //网络数据
    private MagSendNoticeInfo magSendNoticeInfo;
    //公告标题
    private EditText mEt_notice_title;
    //公告内容
    private EditText mEt_notice_content;
    //驳回人员列表存储
    ArrayList<MessageAllUserInfo.ApiParamObjEntity> list=new ArrayList<MessageAllUserInfo.ApiParamObjEntity>();
//    //添加接收人员
//    private LinearLayout mLl_add_person;
    //添加接收人员数目
    private ImageView iv_add_person;
    //显示接收人员数目
    private TextView tv_add_person;
    //发送
    private LinearLayout ll_send;
    private LinearLayout mGallery;
    private LayoutInflater mInflater;
    //显示销售员头像
//    private GridView gv_add_person;
    private HorizontalScrollView horizontalScrollView;
    //头像数据
    private List<Integer> mDatas;
    //意图
    private Intent mIntent;
    //请求码
    private int requestCode;
    //销售员ID集合
    String UserIDs;
    //用户ID
    String UserID;
    //适配器
//    private HorizontalScrollViewAdapter horizontalScrollViewAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_notice);
        //初始化数据
        mIv_Back = (ImageView) findViewById(R.id.iv_Back);
        mEt_notice_title = (EditText) findViewById(R.id.et_notice_title);
        mEt_notice_content = (EditText) findViewById(R.id.et_notice_content);
        mInflater = LayoutInflater.from(this);
        //初始化适配器
        //填充数据
//        mLl_add_person = (LinearLayout) findViewById(R.id.ll_add_person);
        iv_add_person= (ImageView) findViewById(R.id.iv_add_person);
        tv_add_person= (TextView) findViewById(R.id.tv_add_person);
        ll_send = (LinearLayout) findViewById(R.id.ll_send);
        //设置点击事件
        mIv_Back.setOnClickListener(this);
        iv_add_person.setOnClickListener(this);
        ll_send.setOnClickListener(this);
        //
        mGallery = (LinearLayout) findViewById(R.id.id_gallery);

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //购买用途返回数据
        Bundle extras = data.getExtras();
        list= (ArrayList<MessageAllUserInfo.ApiParamObjEntity>) extras.getSerializable("sales");
        // 根据上面发送过去的请求码来区别
        switch (requestCode) {
            case 0:
                for (int i = 0; i < list.size(); i++) {
                    if(list.get(i).getAvatar()!=null) {
                        for (int y = 0; y < list.size(); y++) {
                            View view = mInflater.inflate(R.layout.gridview_sales_person,
                                    mGallery, false);
                            ImageView img = (ImageView) view
                                    .findViewById(R.id.iv_salesman_head);
                            Glide.with(UIUtils.getContext())
                                    .load(UrlUtils.ImageIP + list.get(y).getAvatar())
                                    .into(img);
                            tv_add_person.setText(String.valueOf(list.size()));
                            mGallery.addView(view);
                        }
                    }else{
                        UIUtils.showToastSafe("暂无图片");
                    }
                }
                break;
            default:
                break;
        }
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }
    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_Back:
                showCancelWriteDialog();
                //设置滑动菜单不运行滑动
                break;
            case R.id.iv_add_person:
                mIntent = new Intent();
                mIntent.setClass(UIUtils.getContext(), AddRecPeoAct.class);
                // 请求码的值随便设置，但必须>=0
                requestCode = 0;
                startActivityForResult(mIntent, requestCode);
                break;
            case R.id.ll_send:
                //得到用户输入的信息
                String noticeTitle=mEt_notice_title.getText().toString();
                String noticeContent=mEt_notice_content.getText().toString();
                if (CommonUtil.isEmpty(noticeTitle)) {
                    UIUtils.showToastSafe("请输入公告标题");
                } else if(CommonUtil.isEmpty(noticeContent)) {
                    UIUtils.showToastSafe( "请输入公告内容");
                }else if (tv_add_person.getText().equals("0")) {
                    UIUtils.showToastSafe( "请添加接收人员");
                }else {
                    //初始化Volley
                    if(list.size()!=0) {
                        UserIDs = list.get(0).getID();
                    }
                    UserID= SPUtils.getString(UIUtils.getContext(),"UserID","");
                    String strURL = String.format(UrlUtils.MAG_SEND_NOTICE+"&UserID="+UserID+"&UserIDs="+UserIDs);
                    StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            magSendNoticeInfo = JSON.parseObject(response, MagSendNoticeInfo.class);
                            //第一次
                            if (magSendNoticeData == null) {
                                magSendNoticeData = new ArrayList<MagSendNoticeInfo.ApiParamObjEntity>();
                            }
                            magSendNoticeData.addAll(magSendNoticeInfo.getApiParamObj());
                            UIUtils.runInMainThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(magSendNoticeData.get(0).getIsok()==1){
                                        UIUtils.showToastSafe("公告已发送");
                                    }
                                }
                            });
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                            System.out.println("失败" + error);
                            UIUtils.showToastSafe("请检查网络");
                        }
                    });
                    // Add the request to the RequestQueue.
                    VolleyUtil.getRequestQueue().add(stringRequest);
                    UIUtils.showToastSafe("公告已发送");
                }
                break;
        }
    }
    private void showCancelWriteDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_cancel_write_notice, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        final TextView mTvFolType = (TextView) view.findViewById(R.id.tv_isfollow);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //系统返回键监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK )

        {
            showCancelWriteDialog();
        }
        return false;
    }
}
